﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;
using Microsoft.CSharp.RuntimeBinder;
using Microsoft.Office.Interop.Excel;

namespace YArchitech.HVAC
{
	public partial class FrmCountResult : System.Windows.Forms.Form
	{
		public FrmCountResult()
		{
			this.InitializeComponent();
		}

		private void BindData()
		{
			this.dataGridView1.Width = 558;
			this.dataGridView1.Height = 23;
			base.Width = 590;
			base.Height = this.dataGridView1.Height + 24 + 30;
			this.dataGridView1.RowHeadersVisible = false;
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.Name = "selLength";
			dataGridViewTextBoxColumn.HeaderText = "长度";
			dataGridViewTextBoxColumn.Width = 111;
			dataGridViewTextBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
			this.dataGridView1.Columns.Insert(0, dataGridViewTextBoxColumn);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn2 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn2.Name = "selNum";
			dataGridViewTextBoxColumn2.HeaderText = "数量";
			dataGridViewTextBoxColumn2.Width = 111;
			dataGridViewTextBoxColumn2.SortMode = DataGridViewColumnSortMode.NotSortable;
			this.dataGridView1.Columns.Insert(0, dataGridViewTextBoxColumn2);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn3 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn3.Name = "selSpec";
			dataGridViewTextBoxColumn3.HeaderText = "规格";
			dataGridViewTextBoxColumn3.Width = 111;
			dataGridViewTextBoxColumn3.SortMode = DataGridViewColumnSortMode.NotSortable;
			this.dataGridView1.Columns.Insert(0, dataGridViewTextBoxColumn3);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn4 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn4.Name = "selMater";
			dataGridViewTextBoxColumn4.HeaderText = "材质";
			dataGridViewTextBoxColumn4.Width = 111;
			dataGridViewTextBoxColumn4.SortMode = DataGridViewColumnSortMode.NotSortable;
			this.dataGridView1.Columns.Insert(0, dataGridViewTextBoxColumn4);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn5 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn5.Name = "selName";
			dataGridViewTextBoxColumn5.HeaderText = "选择集";
			dataGridViewTextBoxColumn5.Width = 111;
			dataGridViewTextBoxColumn5.SortMode = DataGridViewColumnSortMode.NotSortable;
			this.dataGridView1.Columns.Insert(0, dataGridViewTextBoxColumn5);
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(XmlInfo.xmlCountResult);
			XmlNodeList xmlNodeList = xmlDocument.SelectNodes("Elements/Select[@Check='true']");
			XmlNodeList xmlNodeList2 = xmlDocument.SelectNodes("Elements/Select[@Check='true']/Mater/Spec");
			this.dataGridView1.Rows.Clear();
			if (xmlNodeList2.Count == 0)
			{
				return;
			}
			this.dataGridView1.Rows.Add(xmlNodeList2.Count);
			for (int i = 0; i < xmlNodeList2.Count; i++)
			{
				this.dataGridView1.Rows[i].Cells[0].Value = xmlNodeList2.Item(i).ParentNode.ParentNode.Attributes["Name"].Value;
				this.dataGridView1.Rows[i].Cells[1].Value = xmlNodeList2.Item(i).ParentNode.Attributes["Name"].Value;
				this.dataGridView1.Rows[i].Cells[2].Value = xmlNodeList2.Item(i).Attributes["Name"].Value;
				this.dataGridView1.Rows[i].Cells[3].Value = xmlNodeList2.Item(i).Attributes["Num"].Value;
				this.dataGridView1.Rows[i].Cells[4].Value = xmlNodeList2.Item(i).Attributes["Length"].Value;
			}
			List<int> list = new List<int>();
			List<string> list2 = new List<string>();
			int num = 0;
			for (int j = 0; j < xmlNodeList.Count; j++)
			{
				XmlNodeList xmlNodeList3 = xmlDocument.SelectNodes("Elements/Select[@Name='" + xmlNodeList.Item(j).Attributes["Name"].Value + "']/Mater/Spec");
				if (xmlNodeList3.Count != 0)
				{
					num += xmlNodeList3.Count;
					list.Add(num);
					list2.Add(xmlNodeList.Item(j).Attributes["Name"].Value);
				}
			}
			for (int k = 1; k < xmlNodeList2.Count; k++)
			{
				this.dataGridView1.Rows[k].Cells[0].Value = "";
			}
			for (int l = 0; l < list.Count - 1; l++)
			{
				this.dataGridView1.Rows[list.ElementAt(l)].Cells[0].Value = list2.ElementAt(l + 1);
			}
			this.dataGridView1.Width = 558;
			this.dataGridView1.Height = (xmlNodeList2.Count + 1) * 23;
			base.Width = 590;
			base.Height = this.dataGridView1.Height + 24 + 30 + 40;
			this.textBox1.Focus();
		}

		private void FrmCountResult_Load(object sender, EventArgs e)
		{
			this.BindData();
		}

		private void btnExport_Click(object sender, EventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Filter = "文本文件|*.*|Excel文件|*.xlsx|所有文件|*.*";
			saveFileDialog.FilterIndex = 2;
			saveFileDialog.RestoreDirectory = true;
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				try
				{
					this.ExportExcel(saveFileDialog.FileName);
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.ToString());
				}
			}
		}

		private void ExportExcel(string outFileName)
		{
			string text = Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)) + "\\" + "Res\\countTemplate.xlsx";
			Path.GetFileName(text);
			new FileInfo(text).CopyTo(outFileName, true);
			object value = Missing.Value;
			Microsoft.Office.Interop.Excel.Application application = (Microsoft.Office.Interop.Excel.Application)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("00024500-0000-0000-C000-000000000046")));
			Workbook workbook = application.Workbooks.Open(outFileName, false, value, value, value, value, value, value, value, value, value, value, value, value, value);
			application.Visible = true;
            //if (FrmCountResult.<>o__4.<>p__0 == null)
            //{
            //    FrmCountResult.<>o__4.<>p__0 = CallSite<Func<CallSite, object, Worksheet>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(Worksheet), typeof(FrmCountResult)));
            //}
            //Worksheet worksheet = FrmCountResult.<>o__4.<>p__0.Target(FrmCountResult.<>o__4.<>p__0, workbook.Worksheets.get_Item(1));
            //int count = this.dataGridView1.Rows.Count;
            //int count2 = this.dataGridView1.Columns.Count;
            //for (int i = 0; i < count2; i++)
            //{
            //    worksheet.Cells[1, i + 1] = this.dataGridView1.Columns[i].HeaderText;
            //}
            //for (int j = 0; j < count; j++)
            //{
            //    for (int k = 0; k < count2; k++)
            //    {
            //        worksheet.Cells[j + 2, k + 1] = this.dataGridView1.Rows[j].Cells[k].Value.ToString();
            //    }
            //}
			workbook.Save();
		}
	}
}
